package dk.sensobyg.communication;

import java.io.IOException;
import java.io.OutputStream;
import java.math.BigInteger;

public class Serializer {
	private OutputStream output;
	public Serializer(OutputStream output) {
		this.output = output;
	}
	public void writeByte(byte value) throws IOException {
		output.write((int) value);
	}
	public void writeInt(int value) throws IOException {
		byte b1 = (byte) ((value >> 0) & 0xFF);
		byte b2 = (byte) ((value >> 8) & 0xFF);
		byte b3 = (byte) ((value >> 16) & 0xFF);
		byte b4 = (byte) ((value >> 24) & 0xFF);
		writeByte(b1);
		writeByte(b2);
		writeByte(b3);
		writeByte(b4);
	}
	public void writeBytes(byte[] value) throws IOException {
		writeInt(value.length);
		for (int i = 0; i < value.length; i++) {
			writeByte((byte) value[i]);
		}
	}
	public void writeString(String value) throws IOException {
		writeBytes(value.getBytes());
	}
	public void writeBigInteger(BigInteger value) throws IOException {
		writeBytes(value.toByteArray());
	}
}
